 //
//  NetworkFactory.m
//  kuping
//
//  Created by SUNJUN on 13-3-11.
//  Copyright (c) 2013年 kuping. All rights reserved.
//

#import "NetworkFactory.h"
#import "ASIHTTPRequest.h"
#import "ASIHTTPRequestConfig.h"
#import "ASIFormDataRequest.h"
#import "sSBjson.h"
#import "AppDelegate.h"
#import "ProgressHUD.h"//消息提示弹框

@implementation NetworkFactory

//POST请求服务器默认地
- (void)startPostRequestWithURL:(NSString *)theURL andPostValues:(NSDictionary *)thePostValues andSuccessBlock:(void (^)(NSDictionary *responseJson))completionBlock andFailBlock:(void (^)(NSError *error))failedBlock{
    
    
    NSString  *json=thePostValues[@"Jsons"];
    NSLog(@"输入的参数:%@",json);
    
         // 网络状态判断
    Reachability *connectionNetWork= [Reachability reachabilityForInternetConnection] ;
    NetworkStatus netWorkStatus = [connectionNetWork currentReachabilityStatus ];
    if (netWorkStatus == 0) {
        [[TipsCenter center]addTip:NO_NETWORK_POINT level:ZDTipsLevelNotice];
        return;
    }
    [SVProgessHUDUnity       show ];

    NSString *urlString = [NSString stringWithFormat:@"%@%@",ServiceURL,theURL];
    NSLog(@"请求地址:%@",urlString);
    
    ASIFormDataRequest *rr = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlString]];
     [rr setValidatesSecureCertificate:NO];
    //    ASIHTTPRequest *req                     = [ASIHTTPRequest requestWithURL:url];
    rr.shouldAttemptPersistentConnection = NO;
    for (int i = 0; i < [[thePostValues allKeys] count]; i++) {
        
        NSString  *strKey=[[thePostValues allKeys] objectAtIndex:i];
        if ([strKey isEqualToString:@"image"]) {
             [rr setFile: thePostValues[strKey] forKey: @"image"];
        }
        else{
          [rr setPostValue:[thePostValues objectForKey:[[thePostValues allKeys] objectAtIndex:i]] forKey:[[thePostValues allKeys] objectAtIndex:i]];
        }
      
    }

    __block ASIFormDataRequest *_rr = rr;
    [rr setTimeOutSeconds:TimeOutSeconds];
    [rr setCompletionBlock:^{
        
        NSString *responseString = [_rr responseString];
        NSLog(@"%@",responseString);

        NSData *response=[responseString  dataUsingEncoding:NSUTF8StringEncoding ];
        NSError *error;
        NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
        
        if (error) {
            [CMCCIOTTipsCenter  showErrorWithStatus:error.localizedDescription];
            failedBlock(error);
        }
        else{
            
            if ([weatherDic[@"IsSuccess"]  integerValue]==1) {
                
            }
            else{
               [CMCCIOTTipsCenter showErrorWithStatus:weatherDic[@"message"]];
            }
            completionBlock(weatherDic);
        }

        [SVProgessHUDUnity       disMiss ];
        
    }];
    [rr setFailedBlock:^{
        NSError  *error=[_rr error];
        failedBlock([_rr error]);
        [CMCCIOTTipsCenter  showErrorWithStatus:error.localizedDescription];
        [SVProgessHUDUnity       disMiss ];

    }];
    
    [rr startAsynchronous];
}



//上传图片带进度条
- (void)uploadImageWithURL:(NSString *)theURL withPostValues:(NSDictionary *)thePostValues andData:(NSData *)theData andProgressView:(id)progressView andSuccessBlock:(void (^)(NSDictionary *responseJson))completionBlock andFailBlock:(void (^)(NSError *error))failedBlock{
    
    NSString *urlString = [NSString stringWithFormat:@"%@%@",ServiceURL,theURL];
    NSLog(@"请求地址:%@",urlString);
    
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    
    ASIFormDataRequest *rr = [ASIFormDataRequest requestWithURL:url];
    rr.shouldAttemptPersistentConnection = NO;
    [rr setShowAccurateProgress:YES];
    //    rr.timeOutSeconds = TimeOutSeconds;
    
    [rr setPostValue:@"multipart/form-data" forKey:@"enctype"];
    
    for (int i = 0; i < [[thePostValues allKeys] count]; i++) {
        [rr setPostValue:[thePostValues objectForKey:[[thePostValues allKeys] objectAtIndex:i]] forKey:[[thePostValues allKeys] objectAtIndex:i]];
    }
    
    [rr addData:theData withFileName:@"jwwheadpic.jpg" andContentType:@"" forKey:@"pic"];
    NSLog(@"%ld",[theData length]);
    __block ASIFormDataRequest *_rr = rr;
    [rr setTimeOutSeconds:TimeOutSeconds];
    [rr setCompletionBlock:^{
        NSLog(@"%@",[_rr responseString]);
        completionBlock([[_rr responseString] JSONValue]);
    }];
    [rr setFailedBlock:^{
        failedBlock([_rr error]);
    }];
    [rr setUploadProgressDelegate:progressView];
    
    
    [rr startAsynchronous];
}

@end
